package com.mcbbs.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.mcbbs.web.entity.Users;
import com.mcbbs.web.service.UsersService;
import com.mcbbs.web.service.impl.UsersSilverCoinServiceImpl;
import com.mcbbs.web.util.SHA256;
import com.mcbbs.web.util.SendEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UsersController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private SendEmail sendEmail;
    @Autowired
    private UsersSilverCoinServiceImpl userCoinService;
    @RequestMapping("/login.html")
    public ModelAndView loginView(){
        ModelAndView mav  = new ModelAndView("login-page");
        return  mav;
    }


    @RequestMapping("/login")
    @ResponseBody
    public String login(Users users, HttpSession session){
        JSONObject json = new JSONObject();
        Users login = usersService.login(users.getUsername(), users.getPassword());
        if(login!=null){
            session.setAttribute("user",login);
            json.put("state",1);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }

//    注册
    @RequestMapping("/reg")
    @ResponseBody
    public String reg(Users users, String code, HttpSession session, HttpServletRequest request){
        JSONObject json = new JSONObject();
        Users usersByName = usersService.getUsersByName(users.getRealname().toLowerCase());
        if(usersByName==null){
            String syscode =(String) session.getAttribute("code");
            if(syscode!=null&&code!=null&&code.equals(code)){
//                设置用户的注册ip
                users.setRegip(request.getRemoteAddr());
//                转换名称大小写
                users.setUsername(users.getRealname().toLowerCase());
//                添加这个用户
                int i = usersService.regUsers(users);
                json.put("state",i);
                if(i==1){
//                    判断是否注册成功,如果注册成功那么将用户的信息添加到会话中
                    session.setAttribute("user",usersService.getUsersByName(users.getUsername()));
                }
            }else{
//                验证码输入错误
                json.put("state",-2);
            }
        }else{
//            已经存在该用户,该昵称已经背时用
            json.put("state",-1);
        }
        return json.toString();
    }
//    发送注册邮箱验证码
    @RequestMapping("/sendRegEmail")
    @ResponseBody
    public String sendRegEmail(String email,HttpSession session){
        JSONObject json = new JSONObject();
        Users usersByEmail = usersService.getUsersByEmail(email);
        if(usersByEmail==null){
//            生成验证码
            String code = SHA256.getRandomSalt(4);
//            将验证码加入到会话中去
            session.setAttribute("code",code);
//            设置该会话的存活时间5分钟
            session.setMaxInactiveInterval(5*60);
//            发送验证码
            boolean b = sendEmail.sendRegEmail(email, code);
            if(b){
//                已经发送成功了
                json.put("state",1);
            }else {
//                发送失败请检查您的邮箱填写是否正确
                json.put("state",-2);
            }
        }else{
//        改邮箱已经被注册
            json.put("state",-1);
        }
        return json.toString();
    }

    @RequestMapping("/test")
    @ResponseBody
    public String test(HttpServletRequest request){
        JSONObject json = new JSONObject();
        String ip = request.getRemoteAddr();
        json.put("ip",ip);
        return json.toJSONString();
    }

}
